﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using RM2012Server.Model;
using System.Data;
using RM2012Server.Business;
using DevComponents.DotNetBar.Controls;
using System.Windows.Forms;

namespace RM2012Server.Controller
{
    public class MenuCtrl
    {
        MenuDAO m_MenuDAO = new MenuDAO();

        public void LoadToComboBox(ComboBoxEx cmb)
        {
            cmb.DataSource = GetAll();
            cmb.DisplayMember = "MenuName";
            cmb.ValueMember = "MenuId";
        }

        public void LoadToDataGridViewComboBoxColumn(DataGridViewComboBoxColumn cmb)
        {
            cmb.DataSource = GetAll();
            cmb.DisplayMember = "MenuName";
            cmb.ValueMember = "MenuId";
            cmb.DataPropertyName = "MenuId";
            cmb.HeaderText = "Món ăn";
        }

        public void LoadToDataGridViewComboBoxColumnState(DataGridViewComboBoxColumn cmb)
        {
            cmb.DataSource = MenuStateSource();
            cmb.DisplayMember = "StateName";
            cmb.ValueMember = "State";
            cmb.DataPropertyName = "State";
            cmb.HeaderText = "Trạng thái";
        }

        public List<MenuStateInfo> MenuStateSource()
        {
            List<MenuStateInfo> list = new List<MenuStateInfo>();
            list.Add(new MenuStateInfo(0, "Hết"));
            list.Add(new MenuStateInfo(1, "Còn"));
            return list;
        }

        public void Show(DataGridViewX dgv,
                         BindingNavigator bn,
                         ComboBoxEx cmbCate,
                         TextBoxX txtMenuId,
                         TextBoxX txtMenuName,
                         TextBoxX txtPrice,
                         ComboBoxEx cmbUnit,
                         ComboBoxEx cmbState,
                         TextBoxX txtRemark)
        {
            BindingSource bs = new BindingSource();
            bs.DataSource = GetAll();

            cmbCate.DataBindings.Clear();
            cmbCate.DataBindings.Add("SelectedValue", bs, "CateId");

            txtMenuId.DataBindings.Clear();
            txtMenuId.DataBindings.Add("Text", bs, "MenuId");

            txtMenuName.DataBindings.Clear();
            txtMenuName.DataBindings.Add("Text", bs, "MenuName");

            txtPrice.DataBindings.Clear();
            txtPrice.DataBindings.Add("Text", bs, "Price");

            cmbUnit.DataBindings.Clear();
            cmbUnit.DataBindings.Add("SelectedValue", bs, "UnitId");

            cmbState.DataBindings.Clear();
            cmbState.DataBindings.Add("SelectedIndex", bs, "State");

            txtRemark.DataBindings.Clear();
            txtRemark.DataBindings.Add("Text", bs, "Remark");

            bn.BindingSource = bs;
            dgv.DataSource = bs;
        }

        public void ShowWithSource(DataGridViewX dgv,
                         BindingNavigator bn,
                         ComboBoxEx cmbCate,
                         TextBoxX txtMenuId,
                         TextBoxX txtMenuName,
                         TextBoxX txtPrice,
                         ComboBoxEx cmbUnit,
                         ComboBoxEx cmbState,
                         TextBoxX txtRemark,
                         DataTable dtSources)
        {
            BindingSource bs = new BindingSource();
            bs.DataSource = dtSources;

            cmbCate.DataBindings.Clear();
            cmbCate.DataBindings.Add("SelectedValue", bs, "CateId");

            txtMenuId.DataBindings.Clear();
            txtMenuId.DataBindings.Add("Text", bs, "MenuId");

            txtMenuName.DataBindings.Clear();
            txtMenuName.DataBindings.Add("Text", bs, "MenuName");

            txtPrice.DataBindings.Clear();
            txtPrice.DataBindings.Add("Text", bs, "Price");

            cmbUnit.DataBindings.Clear();
            cmbUnit.DataBindings.Add("SelectedValue", bs, "UnitId");

            cmbState.DataBindings.Clear();
            cmbState.DataBindings.Add("SelectedIndex", bs, "State");

            txtRemark.DataBindings.Clear();
            txtRemark.DataBindings.Add("Text", bs, "Remark");

            bn.BindingSource = bs;
            dgv.DataSource = bs;
        }

        public DataTable GetAll()
        {
            return m_MenuDAO.GetAll();
        }

        public DataTable GetById(String menuId)
        {
            return m_MenuDAO.GetById(menuId);
        }

        public DataTable GetByLikeId(String menuId)
        {
            return m_MenuDAO.GetByLikeId(menuId);
        }

        public DataTable GetByName(String menuName)
        {
            return m_MenuDAO.GetByName(menuName);
        }

        public DataTable GetByCate(String cateId)
        {
            return m_MenuDAO.GetByCate(cateId);
        }

        public DataTable GetByState(int state)
        {
            return m_MenuDAO.GetByState(state);
        }

        public DataTable GetByPrice(Double start, Double end)
        {
            return m_MenuDAO.GetByPrice(start, end);
        }

        public String GetUnitId(String menuId)
        {
            return m_MenuDAO.GetUnitId(menuId);
        }

        public DataTable GetByOption(String menuId, String menuName, String cateId, String unitId, bool price, double minPrice, double maxPrice)
        {
            return m_MenuDAO.GetByOption(menuId, menuName, cateId, unitId, price, minPrice, maxPrice);
        }

        public int Add(MenuInfo info)
        {
            return m_MenuDAO.Add(info);
        }

        public int Update(MenuInfo info)
        {
            return m_MenuDAO.Update(info);
        }

        public int Delete(String menuId)
        {
            return m_MenuDAO.Delete(menuId);
        }

        public DataRow AddNewRow()
        {
            return m_MenuDAO.AddNewRow();
        }

        public void AddNewMenu(DataRow m_Row)
        {
            m_MenuDAO.AddNewMenu(m_Row);
        }

        public bool SaveData()
        {
            return m_MenuDAO.SaveData();
        }
    }
}
